﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadHabit.Factory.Controls
{
    /// <summary>
    /// Interaction logic for ReportToolBar.xaml
    /// </summary>
    public partial class ReportToolBar : UserControl {

        private string[] reportTypes = null;

        // this is a key for the TaskCount property
        internal static readonly DependencyPropertyKey SelectedReportTypeKey =
            DependencyProperty.RegisterReadOnly("SelectedReportType", typeof(string), typeof(ReportToolBar), new PropertyMetadata(null));
        public static readonly DependencyProperty SelectedReportTypeProperty = SelectedReportTypeKey.DependencyProperty;



        /// <summary>
        /// Default constructor.
        /// </summary>
        public ReportToolBar()  {
            InitializeComponent();
        }

        /// <summary>
        /// Gets and Sets the viewer.
        /// </summary>
        public ReportsPanel ReportPanel { get; set; }


        public string SelectedReportType {
            get {
                return (string)GetValue(SelectedReportTypeProperty);
            }
        }

        /// <summary>
        /// Prints the current document.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Print_Click(object sender, RoutedEventArgs e)  {
            if (ReportPanel != null)
                ReportPanel.Print();
        }


        /// <summary>
        /// Gets the avaiable reports types.
        /// </summary>
        private string[] ReportsTypes {
            get {
                if (reportTypes == null) {
                    try {
                        reportTypes = Client.Service.RetriveHourBankTemplateList();
                    } catch (Exception ex) {
                        Windows.Error.ShowError(ex, "Sorry, could not read available reports");
                    }
                }
                return reportTypes;
            }
        }

        /// <summary>
        /// Loads the report types.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            reportTypeListBox.ItemsSource = ReportsTypes;
            
        }

        private void reportTypeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            SetValue(SelectedReportTypeKey, (string) reportTypeListBox.SelectedItem);
            ReportPanel.ReportType = SelectedReportType;            
        }
    }
}
